package ICM.client;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.List;
import java.awt.SystemColor;
import javax.swing.JLabel;
import javax.swing.JButton;

import common.CommandMessage;
import common.Request;
import common.TextMessage;
import common.User;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

import javax.swing.JComboBox;
import javax.swing.table.DefaultTableModel;


/**
 * Treatment request for system engineer.
 */
public class SystemEngineerUI extends JPanel implements ActionListener, Observer {
	private JButton btnOk;
	private JButton btnCancel;
	private ClientUI ui;
	private Client c;
	private  JComboBox comboBox;
	private DefaultTableModel defaultModel = null;
	private Vector newRow = null;
	private JTable table;
	AppointmentUI ap;
	public static String[] info=new String[3];
	
	ArrayList<String[]> reqList = new ArrayList<String[]>();

	
	
	public SystemEngineerUI(Client c, ClientUI ui) {
		
		this.c=c;
		this.ui=ui;
		c.addObserver(this);
		setLayout(null);
		
		
		CommandMessage msg = new CommandMessage("Get engineer requests",new Integer[] {Integer.parseInt(Client.Login)});
		// requests.
		try {
			c.sendToServer(msg);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
		    Thread.sleep(800);
		} catch(InterruptedException ex) {
		    Thread.currentThread().interrupt();
		}
		
		
		JLabel lblUserid = new JLabel("Your ID is: ");
		lblUserid.setBounds(85, 72, 80, 14);
		add(lblUserid);
		
		JLabel lblMonitoringRequest = new JLabel("System Engineer Window");
		lblMonitoringRequest.setBounds(410, 24, 200, 20);
		add(lblMonitoringRequest);
		
		JLabel active = new JLabel("Requests in which you have some role");
		active.setBounds(85, 40, 240, 20);
		add(active);
		
		
		btnOk = new JButton("OK");
		btnOk.setBounds(490, 423, 89, 23);
		add(btnOk);
		btnOk.addActionListener( this);
		
		btnCancel = new JButton("Cancel");
		
		btnCancel.setBounds(360, 423, 89, 23);
	
		btnCancel.addActionListener( this);
		add(btnCancel);
	
		table = new JTable();
		table.setModel(new DefaultTableModel(
				new Object[][] {
				},
				new String[] {
					"Request ID", "Role","Status"
				}
			));
			defaultModel = (DefaultTableModel) table.getModel();
			
			
	        if (reqList!= null)
	        {
	        	newRow = new Vector();      	
	        	for (int j=0;j<reqList.size();j++)       		
	        		     defaultModel.addRow(new Object[]{reqList.get(j)[0],reqList.get(j)[1],reqList.get(j)[2]});       		 
	        }
							
			JScrollPane pane = new JScrollPane(table); 
			pane.setBounds(41, 124, 867, 226);
			add(pane);
			
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					JTable target = (JTable)e.getSource();
					int row = target.getSelectedRow();
					info[0]=(String) table.getValueAt(row,0);
					info[1]=(String) table.getValueAt(row,1);
					info[2]=(String) table.getValueAt(row,2);
				}
			});
					
		JLabel lblNewLabel = new JLabel(Client.Login);
		lblNewLabel.setBounds(192, 72, 80, 14);
		add(lblNewLabel);
		
	}
	
		@Override
		public void actionPerformed(ActionEvent e) 
		{
			try {
    		    Thread.sleep(1000);
    		} catch(InterruptedException ex) {
    		    Thread.currentThread().interrupt();
    		}
			
			if (e.getSource().equals(btnOk)) 
			{

<<<<<<< .mine
               if (info[1].equals("Supervisor") && info[2].equals("Processing"))
               {
            	   try {
            		   ui.SetContent(ui.Appointment_Screen);
            	   		} 
            	   catch (Exception e1)
            	   		{
					// TODO Auto-generated catch block
					e1.printStackTrace();
            	   		}
               }
               if (info[1].equals("Supervisor") && info[2].equals("Active"))
               {
            	   try {
            		   ui.SetContent(ui.Appointment_Screen);
            	   		} 
            	   catch (Exception e1)
            	   		{
					// TODO Auto-generated catch block
					e1.printStackTrace();
            	   		}
               }
               else if(info[1].equals("Assessor") &&  info[2].equals("Working"))
=======
               if (info[1].equals("Supervisor"))
>>>>>>> .r300
               {
<<<<<<< .mine
            	    try{
            	    	ui.SetContent(ui.Assessor_Screen);
            	    	} 
            	    catch (Exception e1)
            	    {
					// TODO Auto-generated catch block
					e1.printStackTrace();
            	    }
=======
					try {
						ui.SetContent(ui.Appointment_Screen);
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
>>>>>>> .r300
               }
<<<<<<< .mine
               else if(info[1].equals("Assessor") && info[2].equals("Active"))
=======
               else if(info[1].equals("Assessor"))
>>>>>>> .r300
               {
	               try{
	            	   ui.SetContent(ui.Assessor_Screen);
	               } catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
	
	               }
               }
               else if (info[1].equals("Committee"))
               {
   				try {
   					ui.SetContent(ui.Chairman_Screen);
   				} catch (Exception e1) {
   					// TODO Auto-generated catch block
   					e1.printStackTrace();
<<<<<<< .mine
   				} 
			    
        	    try {
        		    Thread.sleep(1000);
        		} catch(InterruptedException ex) {
        		    Thread.currentThread().interrupt();
        		}
               }
        	   
			    
		    }	

			    else if (info[1].equals("Committee") && info[2].equals("Active"))
=======
   				}  
			    else if (info[1].equals("Conductor of performance"))
>>>>>>> .r300
				{
	   				try {
	   					ui.SetContent(ui.Execution_Screen);
	   				} catch (Exception e1) {
	   					// TODO Auto-generated catch block
	   					e1.printStackTrace();
	   				} 
			    }
			    else if (info[1].equals("Tester"))
				{
	   				try {
	   					ui.SetContent(ui.Testing_Screen);
	   				} catch (Exception e1) {
	   					// TODO Auto-generated catch block
	   					e1.printStackTrace();
	   				} 
			    }
			    else if(info[1].equals("Supervisor(closing)"))
	               {
		               try{
		            	   ui.SetContent(ui.Supervisor_Screen);
		               } catch (Exception e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
		
						}
	               }
			}
			    if(e.getSource().equals(btnCancel))
					try {
						ui.SetContent(ClientUI.Main_Screen);
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
<<<<<<< .mine
					} 		
			
=======
					} 
				
				 try {
	        		    Thread.sleep(1000);
	        		} catch(InterruptedException ex) {
	        		    Thread.currentThread().interrupt();
	        		}
>>>>>>> .r300
	 }
		
		/**
		 * Handle massage from Observable class in our case Client
		 */
		@Override
		public void update(Observable arg0, Object arg1) {
			
			String msg = "";
			if (arg1 instanceof TextMessage)
				msg = ((TextMessage)arg1).ToDebugMessage();
			else if (arg1 instanceof CommandMessage) {
				CommandMessage ms=(CommandMessage)arg1;
				//Searching for user active requests 
				if(ms.getCommand().equals("Role In Request"))				
					reqList = (ArrayList<String[]>) ms.getParams()[1];
			}
			else
				msg = arg1.toString();
		}	
}